package com.warmlight.voicepacket.http;

import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.warmlight.voicepacket.BuildConfig;
import com.warmlight.voicepacket.data.eventbus.Login;
import com.warmlight.voicepacket.utils.JSONUtils;
import com.warmlight.voicepacket.utils.MD5Utils;
import com.warmlight.voicepacket.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.UUID;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by jason on 2018/5/16.
 */

public class HttpClient {

    //TODO 添加公共参数开关
    //TODO 消化内部Exception保证外部安全，做保护
    //TODO 抽取OKHttpClient
    public static final long DEFAULT_MILLISECONDS = 10_000L;
    private volatile static HttpClient mInstance;
    private OkHttpClient mOkHttpClient;
//    private Platform mPlatform;

    private HttpClient(OkHttpClient okHttpClient)
    {
        if (okHttpClient == null)
        {
            mOkHttpClient = new OkHttpClient();
        } else
        {
            mOkHttpClient = okHttpClient;
        }

//        mPlatform = Platform.get();
    }

    public static HttpClient getInstance()
    {
        return initClient(null);
    }



    public static HttpClient initClient(OkHttpClient okHttpClient)
    {
        if (mInstance == null)
        {
            synchronized (HttpClient.class)
            {
                if (mInstance == null)
                {
                    mInstance = new HttpClient(okHttpClient);
                }
            }
        }
        return mInstance;
    }

    /**
     * 异步post请求含解密
     */
    public static void post(String url, TreeMap<String, String> map, final com.warmlight.voicepacket.http.Callback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        addCommonParams(map);

        FormBody.Builder buildBody = new FormBody.Builder();
        for (TreeMap.Entry<String, String> e : map.entrySet()) {
            Object value = e.getValue();
            String key = e.getKey();
            if (value != null && key != null) {
                buildBody.add(key, value.toString());
            }
        }

        FormBody body = buildBody.build();
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        okHttpClient.newCall(request).enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callback.onFailure(call,e);
            }

            @Override
            public void onResponse(Call call, Response response){
                String json = "";
                try {
                    String result = response.body().string();
//                    Log.e("onResponse","result:"+result);
                    HttpData data = new Gson().fromJson(result,HttpData.class);
                    String decode = new AES(data.getTime()+"").decrypt(data.getData());
//                    Log.e("decode","decode:"+decode);
                    //拼接，防止server改key，value顺序
                    json = "{\"code\":"+data.getCode()+",\"time\":"+data.getTime()+",\"msg\":\""+data.getMsg()+"\",\"data\":"+decode+"}";
                    if (data != null && TextUtils.equals("301",data.getCode()+"")){
//                        new Handler(Looper.getMainLooper()).post(new Runnable() {
//                            @Override
//                            public void run() {
                        SPUtils.logout();
                        EventBus.getDefault().post(new Login(false));
//                            }
//                        });
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    callback.onResponse(call,json);
                }catch (Exception e){

                }
            }
        });

    }
//使用方法，
//1，拷贝4个类到相应包下
//2，HttpData为后台加密的统一的数据结构
//3，AES类为加密算法
//4，Callback为了替换方便取名跟原来一样，可能现在已经被改了，替换import的包名即可
//5，返回的response对象改为了String的json，直接解析即可

    /**
     * 加解密
     * @param url
     * @param map
     * @return
     */
    public Observable<ResponseData> postRxAndroidAES(final String url, final TreeMap<String, String> map){
        Observable<ResponseData> observable = Observable.create(new ObservableOnSubscribe<ResponseData>() {
            @Override
            public void subscribe(ObservableEmitter<ResponseData> emitter) throws Exception {
                try {
//                    OkHttpClient okHttpClient = new OkHttpClient();
                    addCommonParams(map);
                    ResponseData responseData = new ResponseData();


                    FormBody.Builder buildBody = new FormBody.Builder();
                    for (TreeMap.Entry<String, String> e : map.entrySet()) {
                        Object value = e.getValue();
                        String key = e.getKey();
                        if (value != null && key != null) {
                            buildBody.add(key, value.toString());
                        }
                    }

                    FormBody body = buildBody.build();
                    Request request = new Request.Builder()
                            .url(url)
                            .post(body)
                            .build();

                    Response response = mOkHttpClient.newCall(request).execute();
                    if (response.isSuccessful()){
                        String result = response.body().string();
                        ArrayList<Map<String, String>> listMapByJson = JSONUtils.getListMapByJson(result);
                        if (listMapByJson.size()>0){
                            Map<String, String> map1 = listMapByJson.get(0);
                            if (TextUtils.equals("200",map1.get("code"))){
                                String data = map1.get("data");//加密时用时间
                                String time = map1.get("time");
                                String decode = new AES(time).decrypt(data);
                                responseData.setCode("200");
                                responseData.setData(decode);
                                //拼接，防止server改key，value顺序
                                String json = "{\"code\":"+map1.get("code")+",\"time\":"+time+",\"msg\":\""+map1.get("msg")+"\",\"data\":"+decode+"}";
                                responseData.setBody(json);
                            }else{
                                responseData.setCode(map1.get("code"));
                                responseData.setData(map1.get("data"));
                                responseData.setMsg(map1.get("msg"));
                                responseData.setBody(result);
                                if (TextUtils.equals("301",map1.get("code"))){
                                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                                        @Override
                                        public void run() {
                                            SPUtils.logout();
                                            EventBus.getDefault().post(new Login(false));
                                        }
                                    });

                                }
                            }
                        }else{
                            responseData.setCode("0");
                            responseData.setData("");
                            responseData.setMsg("数据加载失败");
                            responseData.setBody(result);
                        }
                        emitter.onNext(responseData);
                        emitter.onComplete();
                    }else{
                        emitter.onError(new Throwable(response.body().string()));
                        emitter.onComplete();
                    }



                }catch (Exception e){
//                    responseData.setCode("0");
//                    responseData.setData("");
//                    responseData.setMsg("");
                    emitter.onError(new Throwable(""));
                    emitter.onComplete();
                }
            }
        }).subscribeOn(Schedulers.io());

        return observable;
    }
    /**
     * IM
     * @param url
     * @param map
     * @return
     */
    public Observable<ResponseData> postRxAndroidRONGToken(final String url, final TreeMap<String, String> map){
        Observable<ResponseData> observable = Observable.create(new ObservableOnSubscribe<ResponseData>() {
            @Override
            public void subscribe(ObservableEmitter<ResponseData> emitter) throws Exception {
                try {
                    ResponseData responseData = new ResponseData();
                    if (SPUtils.isLogined()){
                        map.put("userId", SPUtils.getLoginUid());
                        map.put("name",SPUtils.getLoginUser().getNickname());
                        map.put("portraitUri",SPUtils.getLoginUser().getAvator());
                    }
                    FormBody.Builder buildBody = new FormBody.Builder();
                    for (TreeMap.Entry<String, String> e : map.entrySet()) {
                        Object value = e.getValue();
                        String key = e.getKey();
                        if (value != null && key != null) {
                            buildBody.add(key, value.toString());
                        }
                    }
                    String time=String.valueOf(new Date().getTime());

                    Random rand = new Random();
                    int i = rand.nextInt(100);
                    String sing=HttpConfig.IMappSecret+i+time;
//                    String sing="t2RIqCY2kkL"+i+time;
//                    String sing="Cl5T4FulHZ172U"+i+time;测试


                    FormBody body = buildBody.build();
                    Request request = new Request.Builder()
                            .url(url)
                            .addHeader("nonce",String.valueOf(Math.random()))
                            .addHeader("App-key",HttpConfig.IMAppkey)//TODO 暂时放这里
//                            .addHeader("App-key","pvxdm17jp3qir")//TODO 暂时放这里
//                            .addHeader("App-key","x4vkb1qpxfo2k")测试//TODO 暂时放这里 测试
                            .addHeader("Timestamp",time)
                            .addHeader("Signature", MD5Utils.getSha1(sing))
                            .addHeader("appSecret",HttpConfig.IMappSecret)
//                            .addHeader("appSecret","t2RIqCY2kkL")
//                            .addHeader("appSecret","Cl5T4FulHZ172U")测试
                            .addHeader("Content-Type","application/x-www-form-urlencoded,application/json")
                            .post(body)
                            .build();

                    Response response = mOkHttpClient.newCall(request).execute();
                    if (response.isSuccessful()){
                        String result = response.body().string();

                            responseData.setBody(result);
//                        }
                        emitter.onNext(responseData);
                        emitter.onComplete();
                    }else{
                        emitter.onError(new Throwable(response.body().string()));
                        emitter.onComplete();
                    }



                }catch (Exception e){
                    emitter.onError(e);
                    emitter.onComplete();
                }
            }
        }).subscribeOn(Schedulers.io());

        return observable;
    }
//    public Observable<ResponseData> postRxAndroid(final String url, final TreeMap<String, String> map){
//        Observable<ResponseData> stringObservable = Observable.create(new ObservableOnSubscribe<ResponseData>() {
//            @Override
//            public void subscribe(ObservableEmitter<ResponseData> emitter) throws Exception {
//                try {
////                    OkHttpClient okHttpClient = new OkHttpClient();
//                    addCommonParams(map);
//                    ResponseData responseData = new ResponseData();
//
//
//                    FormBody.Builder buildBody = new FormBody.Builder();
//                    for (TreeMap.Entry<String, String> e : map.entrySet()) {
//                        Object value = e.getValue();
//                        String key = e.getKey();
//                        if (value != null && key != null) {
//                            buildBody.add(key, value.toString());
//                        }
//                    }
//
//                    FormBody body = buildBody.build();
//                    Request request = new Request.Builder()
//                            .url(url)
//                            .post(body)
//                            .build();
//
//                    Response response = mOkHttpClient.newCall(request).execute();
//                    if (response.isSuccessful()){
//                        String string = response.body().string();
//                        ArrayList<Map<String, String>> listMapByJson = JSONUtils.getListMapByJson(string);
//                        if (listMapByJson.size()>0){
//                            Map<String, String> map1 = listMapByJson.get(0);
//                            if (TextUtils.equals("200",map1.get("code"))){
//                                String data = map1.get("data");
//                                responseData.setCode("200");
//                                responseData.setData(data);
//                            }else{
//                                responseData.setCode(map1.get("code"));
//                                responseData.setData(map1.get("data"));
//                                responseData.setMsg(map1.get("msg"));
//                            }
//                        }else{
//                            responseData.setCode("0");
//                            responseData.setData("");
//                            responseData.setMsg("");
//                        }
//                        emitter.onNext(responseData);
//                        emitter.onComplete();
//                    }else{
//                        emitter.onError(new Throwable(response.body().string()));
//                        emitter.onComplete();
//                    }
//
//
//
//                }catch (Exception e){
////                    responseData.setCode("0");
////                    responseData.setData("");
////                    responseData.setMsg("");
//                    emitter.onError(e);
//                    emitter.onComplete();
//                }
//            }
//        }).subscribeOn(Schedulers.io());
//
//        return stringObservable;
//    }

    /**
     * 异步post请求
     */
//    public static void post(String url, TreeMap<String, String> map, final Callback callback) {
//        OkHttpClient okHttpClient = new OkHttpClient();
//        addCommonParams(map);
//
//        FormBody.Builder buildBody = new FormBody.Builder();
//        for (TreeMap.Entry<String, String> e : map.entrySet()) {
//            Object value = e.getValue();
//            String key = e.getKey();
//            if (value != null && key != null) {
//                buildBody.add(key, value.toString());
//            }
//        }
//
//        FormBody body = buildBody.build();
//        Request request = new Request.Builder()
//                .url(url)
//                .post(body)
//                .build();
//
//        okHttpClient.newCall(request).enqueue(callback);
//
//    }
    /**
     * 异步post请求，泛型
     */
    public static <T> void post(String url, TreeMap<String, String> map, final Class<T> clazz, @NonNull final HttpCallback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        addCommonParams(map);

        FormBody.Builder buildBody = new FormBody.Builder();
        for (TreeMap.Entry<String, String> e : map.entrySet()) {
            Object value = e.getValue();
            String key = e.getKey();
            if (value != null && key != null) {
                buildBody.add(key, value.toString());
            }
        }

        FormBody body = buildBody.build();
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callback.onFailure(call,e);
            }

            @Override
            public void onResponse(Call call, Response response){

                String result = null;
                try {
                    result = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                T javaBean = null;
                try {
                    javaBean = HttpUtils.json2Bean(result, clazz);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                callback.onResponse(call,response,javaBean);
            }
        });
    }

    /**
     * 同步post请求，泛型
     */
    public static <T> T post(final String url, final TreeMap<String, String> map, final Class<T> clazz){
        OkHttpClient okHttpClient = new OkHttpClient();
        addCommonParams(map);

        FormBody.Builder buildBody = new FormBody.Builder();
        for (TreeMap.Entry<String, String> e : map.entrySet()) {
            Object value = e.getValue();
            String key = e.getKey();
            if (value != null && key != null) {
                buildBody.add(key, value.toString());
            }
        }

        FormBody body = buildBody.build();
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = null;

        try {
            response = okHttpClient.newCall(request).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }

        String result = null;
        try {
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }

        T javaBean = null;

        try {
            javaBean = HttpUtils.json2Bean(result, clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return javaBean;
    }

    /**
     * 同步post请求
     * 增加解密算法，
     */
    public static String post(final String url, final TreeMap<String, String> map){
        OkHttpClient okHttpClient = new OkHttpClient();
        addCommonParams(map);

        FormBody.Builder buildBody = new FormBody.Builder();
        for (TreeMap.Entry<String, String> e : map.entrySet()) {
            Object value = e.getValue();
            String key = e.getKey();
            if (value != null && key != null) {
                buildBody.add(key, value.toString());
            }
        }

        FormBody body = buildBody.build();
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        String json = "";
        try {
            Response response = okHttpClient.newCall(request).execute();


            String result = response.body().string();
            json = result;
//                    Log.e("onResponse","result:"+result);
            HttpData data = new Gson().fromJson(result,HttpData.class);
            String decode = new AES(data.getTime()+"").decrypt(data.getData());
//                    Log.e("decode","decode:"+decode);
            //拼接，防止server改key，value顺序
            json = "{\"code\":"+data.getCode()+",\"time\":"+data.getTime()+",\"msg\":\""+data.getMsg()+"\",\"data\":"+decode+"}";



            return json;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 异步get请求
     */
    public static void get(String url, TreeMap<String, String> map, final Callback callback) {
        //添加公用参数
        OkHttpClient okHttpClient = new OkHttpClient();
        addCommonParams(map);

        StringBuilder sBuilder = HttpStringUtils.getUriParamString(map);
        url = url + "?" + sBuilder.toString();

        Request request = new Request.Builder()
                .url(url)
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }

    /**
     * 异步get请求，泛型
     */
    public static <T> void get(String url, TreeMap<String, String> map, final Class<T> clazz, final HttpCallback callback){
        //添加公用参数

        OkHttpClient okHttpClient = new OkHttpClient();
        addCommonParams(map);

        StringBuilder sBuilder = HttpStringUtils.getUriParamString(map);
        url = url + "?" + sBuilder.toString();

        Request request = new Request.Builder()
                .url(url)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callback.onFailure(call,e);
            }

            @Override
            public void onResponse(Call call, Response response){
                String result = null;
                try {
                    result = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                T javaBean = null;
                try {
                    javaBean = HttpUtils.json2Bean(result, clazz);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                callback.onResponse(call,response,javaBean);
            }
        });
    }

    /**
     * 同步get请求
     */
    public static String get(String url, TreeMap<String, String> map) {
        //添加公用参数
        OkHttpClient okHttpClient = new OkHttpClient();
        addCommonParams(map);

        StringBuilder sBuilder = HttpStringUtils.getUriParamString(map);
        url = url + "?" + sBuilder.toString();

        Request request = new Request.Builder()
                .url(url)
                .build();

        try {
            Response response = okHttpClient.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 同步get请求，泛型
     */
    public static <T> T get(String url, TreeMap<String, String> map, final Class<T> clazz){
        OkHttpClient okHttpClient = new OkHttpClient();
        addCommonParams(map);

        StringBuilder sBuilder = HttpStringUtils.getUriParamString(map);
        url = url + "?" + sBuilder.toString();

        Request request = new Request.Builder()
                .url(url)
                .build();
        Response response = null;

        try {
            response = okHttpClient.newCall(request).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }

        String result = null;

        try {
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }

        T javaBean = null;

        try {
            javaBean = HttpUtils.json2Bean(result, clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return javaBean;
    }



    public void httpTest(){
        OkHttpClient okHttpClient = new OkHttpClient();
        // 创建一个Request
        final Request request = new Request.Builder()
                .url("http://app.nuanguang.vip/api/index/index/commentList")
                .build();
        Call call = okHttpClient.newCall(request);
        // 请求加入调度
        call.enqueue(new Callback() {
            // 请求失败的回调
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            // 请求成功的回调
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                // 将response转化成String
                String responseStr = response.body().string();
//                Log.e("TAG","responseStr:"+responseStr);
            }
        });
    }


    private static TreeMap<String, String> addCommonParams(@NonNull TreeMap<String, String> map){
        map.put("device",getUniquePsuedoID());
        map.put("token",getUniquePsuedoID());
        map.put("platform","android");
        map.put("auth_key", SPUtils.getLoginAuthKey());
        map.put("version", BuildConfig.VERSION_NAME);
        map.put("phonemodel",Build.MODEL);
        return map;
    }
    public static String getUniquePsuedoID() {
        String serial = null;

        String m_szDevIDShort = "35" +
                Build.BOARD.length() % 10 + Build.BRAND.length() % 10 +

                Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10 +

                Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 +

                Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 +

                Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 +

                Build.TAGS.length() % 10 + Build.TYPE.length() % 10 +

                Build.USER.length() % 10; //13 位

        try {
            serial = android.os.Build.class.getField("SERIAL").get(null).toString();
            //API>=9 使用serial号
            return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
        } catch (Exception exception) {
            //serial需要一个初始化
            serial = "serial"; // 随便一个初始化
        }
        //使用硬件信息拼凑出来的15位号码
        return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
    }
//    /**
//     * 下载文件
//     *
//     * @param url             接口地址
//     * @param dbmFileCallback
//    public static void downloadFile(String url, DBMFileCallback dbmFileCallback) {
//
//        OkHttpUtils
//                .get()
//                .url(url)
//                .build()
//                .execute(dbmFileCallback);
//
//    }
//
//
//    *//**
//     * 同步的下载方法
//     *
//     * @param url          接口地址
//     * @param destFileDir  下载后文件保存的地址
//     * @param destFileName 下载后的文件名
//     * @return
//     * @throws IOException
//     *//*
//    public static File downloadFile(String url, String destFileDir, String destFileName) throws Exception {
//
//        Request.Builder builder = new Request.Builder();
//        Request request = builder.url(url).get().build();
//        Response execute = null;
//        try {
//            execute = OkHttpUtils.getInstance().getOkHttpClient().newCall(request).execute();
//        } catch (IOException e) {
//            throw new Exception(e);
//        }
//        if (execute != null) {
//            return HttpUtils.saveFile(execute, destFileDir, destFileName);
//        } else {
//            throw new Exception(new UnknownServiceException("code=" + (execute == null ? "null Response" : String.valueOf(execute.code()))));
//        }
//    }
//
//    *//**
//     * 异步上传文件，上传完毕后callback中回调在主线程
//     *
//     * @param params       传递的普通参数
//     * @param uploadFile   需要上传的文件
//     * @param fileFormName 需要上传文件表单中的名字
//     * @param newFileName  上传的文件名称，不填写将为uploadFile的名称
//     * @param url          上传的服务器的路径
//     * @throws IOException
//     *//*
//    public static void uploadForm(String url, Map<String, String> params, String fileFormName,
//                                  File uploadFile, String newFileName, final WLCallback callback) {
//        if (!uploadFile.exists()) {
//            throw new RuntimeException("要上传的文件不存在!");
//        }
//        if (newFileName == null || newFileName.trim().equals("")) {
//            newFileName = uploadFile.getName();
//        }
//        Map<String, String> headers = new HashMap<>();
//
//        //普通的表单数据
//        if (params != null && params.size() > 0) {
//            for (String key : params.keySet()) {
//                headers.put(key, params.get(key));
//            }
//        }
//        Callback realCallBack = new Callback() {
//
//            @Override
//            public Object parseNetworkResponse(Response response, int id) throws Exception {
//                return callback.parseNetworkResponse(response, id);
//            }
//
//            @Override
//            public void onError(Call call, Exception e, int id) {
//                callback.onError(call, e, id);
//            }
//
//            @Override
//            public void onResponse(Object response, int id) {
//                callback.onResponse(response, id);
//            }
//        };
//        OkHttpUtils.post()//
//                .addFile(fileFormName, newFileName, uploadFile)//
//                .url(url)//
//                .params(params)//
//                .headers(headers)//
//                .build()//
//                .execute(realCallBack);
//
//    }
//
//    *//**
//     * 同步上传文件
//     *
//     * @param url          上传的服务器的路径
//     * @param params       传递的普通参数
//     * @param fileFormName 需要上传文件表单中的名字
//     * @param uploadFile   需要上传的文件
//     * @param newFileName  上传的文件名称，不填写将为uploadFile的名称
//     * @return 服务器返回的response.body().string()
//     * @throws Exception
//     *//*
//    public static String uploadForm(String url, Map<String, String> params, String fileFormName,
//                                    File uploadFile, String newFileName)
//            throws Exception {
//        if (!uploadFile.exists()) {
//            throw new RuntimeException("要上传的文件不存在!");
//        }
//        if (newFileName == null || newFileName.trim().equals("")) {
//            newFileName = uploadFile.getName();
//        }
//        Map<String, String> headers = new HashMap<>();
//
//        //普通的表单数据
//        if (params != null && params.size() > 0) {
//            for (String key : params.keySet()) {
//                headers.put(key, params.get(key));
//            }
//        }
//        try {
//            Response execute = OkHttpUtils.post()//
//                    .addFile(fileFormName, newFileName, uploadFile)//
//                    .url(url)//
//                    .params(params)//
//                    .headers(headers)//
//                    .build()//
//                    .execute();
//            if (execute != null && execute.isSuccessful()) {
//                String result = execute.body().string();
//                return result;
//            } else {
//                throw new Exception(new UnknownServiceException("code=" + (execute == null ? "null Response" : String.valueOf(execute.code()))));
//            }
//        } catch (IOException e) {
//            throw new Exception(new IOException());
//        }
//
//    }
//
//    *//**
//     * 同步上传文件
//     *
//     * @param url          上传的服务器的路径
//     * @param fileFormName 需要上传文件表单中的名字
//     * @param uploadFile   需要上传的文件
//     * @param newFileName  上传的文件名称，不填写将为uploadFile的名称
//     * @return 服务器返回的response.body().string()
//     * @throws Exception
//     *//*
//    public static String uploadForm(String url, String fileFormName, File uploadFile, String newFileName) throws Exception {
//        return uploadForm(url, null, fileFormName, uploadFile, newFileName);
//    }
//
//
//    *//**
//     * 单纯上传文件，没有普通表单参数
//     *
//     * @param url          上传接口地址
//     * @param fileFormName 需要上传文件在表单中的名字
//     * @param uploadFile   需要上传的文件
//     * @param newFileName  上传文件的新名称，不填写将为uploadFile的名称
//     * @param callback     回调
//     * @throws IOException
//     *//*
//    public static void uploadForm(String url, String fileFormName,
//                                  File uploadFile, String newFileName, WLCallback callback) {
//        uploadForm(url, null, fileFormName, uploadFile, newFileName, callback);
//    }*/

}
